using System;
using System.Collections.Generic;
using System.Linq;
using Kiwi.Container.DependencyInjection;
using Kiwi.Container.Lifetime;
using Kiwi.Container.Scope;

namespace Kiwi.Container.Registration
{
    public class CompositeComponentRegistration : IComponentRegistration
    {
        private readonly IEnumerable<IComponentRegistration> _registrations;

        public CompositeComponentRegistration(IEnumerable<IComponentRegistration> registrations)
        {
            _registrations = registrations.ToArray();
        }

        #region IComponentRegistration Members

        public Type ActualType
        {
            get { throw CreateExcecption("ActualType"); }
        }

        public IComponentType ComponentType
        {
            get { throw CreateExcecption("ComponentType"); }
            set { Each(r => r.ComponentType = value); }
        }

        public IComponentScope ComponentScope
        {
            get { throw CreateExcecption("ComponentScope"); }
            set { Each(r => r.ComponentScope = value); }
        }

        public object Tag
        {
            get { throw CreateExcecption("Tag"); }
            set { Each(r => r.Tag = value); }
        }

        public IEnumerateDependentProperties InjectProperties
        {
            get { throw CreateExcecption("InjectProperties"); }
            set { Each(r => r.InjectProperties = value); }
        }

        public IComponentCacheResolver CacheResolver
        {
            get { throw CreateExcecption("CacheResolver"); }
            set { Each(r => r.CacheResolver = value); }
        }

        public ILifetimeManager LifetimeManager
        {
            get { throw CreateExcecption("LifetimeManager"); }
            set { Each(r => r.LifetimeManager = value); }
        }

        public IComponentActivator Activator
        {
            get { throw CreateExcecption("Activator"); }
        }

        #endregion

        private static Exception CreateExcecption(string property)
        {
            return new ApplicationException("Composite registration can only be modified, not inspected.");
        }

        private void Each(Action<IComponentRegistration> action)
        {
            foreach (var registration in _registrations)
            {
                action(registration);
            }
        }
    }
}